package com.yunfeng.supercall.fragment;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.RingtonePreference;
import android.widget.Toast;

import com.ly.quickdev.library.fragment.PreferenceFragment;
import com.umeng.update.UmengUpdateAgent;
import com.umeng.update.UmengUpdateListener;
import com.umeng.update.UpdateResponse;
import com.umeng.update.UpdateStatus;
import com.yunfeng.supercall.R;
import com.yunfeng.supercall.activity.BaseActivity;
import com.yunfeng.supercall.activity.FeedBackActivity;

public class SettingFragment extends PreferenceFragment {

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		addPreferencesFromResource(R.xml.setting);
		findPreference("feedback").setOnPreferenceClickListener(
				new OnPreferenceClickListener() {

					@Override
					public boolean onPreferenceClick(Preference preference) {
						startActivity(new Intent(getActivity(),
								FeedBackActivity.class));
						return true;
					}
				});
		Preference preference = findPreference("update");
		preference.setSummary(getString(R.string.setting_version,
				BaseActivity.getVersionName(getActivity())));
		final RingtonePreference preference2 = (RingtonePreference) findPreference("music");
		final SharedPreferences sp = preference2.getPreferenceManager()
				.getSharedPreferences();
		try {
			preference2.setSummary(getRingtonName(Uri.parse(sp.getString(
					"alarm", getString(R.string.follow_system)))));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			preference2.setSummary(R.string.follow_system);
		}
		preference2
				.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {

					@Override
					public boolean onPreferenceChange(Preference preference,
							Object newValue) {
						// TODO Auto-generated method stub
						preference2.setSummary(getRingtonName(Uri
								.parse(newValue.toString())));

						sp.edit().putString("alarm", newValue.toString())
								.commit();
						setVoice(Uri.parse(newValue.toString()));
						return false;
					}
				});
		final Context mContext = getActivity();
		findPreference("update").setOnPreferenceClickListener(
				new OnPreferenceClickListener() {

					@Override
					public boolean onPreferenceClick(Preference preference) {
						// TODO Auto-generated method stub
						UmengUpdateAgent
								.setUpdateListener(new UmengUpdateListener() {

									@Override
									public void onUpdateReturned(
											int updateStatus,
											UpdateResponse arg1) {
										// TODO Auto-generated method stub
										if (getActivity() == null) {
											return;
										}
										switch (updateStatus) {
										case UpdateStatus.Yes: // has update
											UmengUpdateAgent.showUpdateDialog(
													mContext, arg1);
											break;
										case UpdateStatus.No: // has no update
											Toast.makeText(mContext,
													R.string.no_update,
													Toast.LENGTH_SHORT).show();
											break;
										case UpdateStatus.NoneWifi: // none wifi
											Toast.makeText(mContext,
													R.string.update_only_wifi,
													Toast.LENGTH_SHORT).show();
											break;
										case UpdateStatus.Timeout: // time out
											Toast.makeText(mContext,
													R.string.time_out,
													Toast.LENGTH_SHORT).show();
											break;
										}
									}
								});
						UmengUpdateAgent.forceUpdate(getActivity());
						return true;
					}
				});

		// ListPreference mListPreference = (ListPreference)
		// findPreference("font");
		// mListPreference.setValueIndex(0);
		// mListPreference
		// .setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
		//
		// @Override
		// public boolean onPreferenceChange(Preference preference,
		// Object newValue) {
		// // TODO Auto-generated method stub
		// return true;
		// }
		// });
	}

	public String getRingtonName(Uri uri) {
		Ringtone r = RingtoneManager.getRingtone(getActivity(), uri);
		return r.getTitle(getActivity());
	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		UmengUpdateAgent.setUpdateListener(null);
	}

	private void setVoice(Uri path2)

	{

		RingtoneManager.setActualDefaultRingtoneUri(getActivity(),
				RingtoneManager.TYPE_RINGTONE, path2);
	}
}
